package com.meihua.baoleyi.base;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;
import android.widget.Toast;

/**
 * Created by liming on 2017/10/16.
 */
public abstract class BaseMyAdapter extends BaseAdapter {

    private Context context;
    private Toast mToast;
    public LayoutInflater inflater;
    public BaseMyAdapter(Context context) {
        this.context=context;
        inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * 直接跳转到另一个activity
     * @param classObj 要跳转的activity
     */
    public void overlay(Class<?> classObj) {
        Intent intent = new Intent(context, classObj);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    public void overlay(Class<?> classObj, Bundle bundle) {
        Intent intent = new Intent(context, classObj);
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * Toast
     * @param msg 文字信息
     */
    public void toast(String msg) {
        mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        mToast.show();
    }
    /**
     * Toast
     * @param resId 资源信息
     */
    public void toast(int resId) {
        String msg =context.getResources().getString(resId);
        mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        mToast.show();
    }

    /**
     * 拨打电话
     * @param phone
     */
    public void callPhone(String phone ){

        if (Build.VERSION.SDK_INT >= 23) {
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);
            if(checkCallPhonePermission != PackageManager.PERMISSION_GRANTED){
                ActivityCompat.requestPermissions((Activity) context,new String[]{Manifest.permission.CALL_PHONE},123);
                return;
            }else{
                Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phone));
                context.startActivity(intent);
            }
        } else {
            Intent intent=new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+phone));
            context.startActivity(intent);
        }
    }
}
